<template>
  <el-dialog v-model="dialogVisible" title="新增/编辑货物" width="70%" :before-close="handleClose">
    <el-form :model="formInline" label-width="120" :rules="rules" ref="formEl">
      <el-form-item label="货物编码" prop="cargoSn">
        <selectWarehousingCargo val="cargoSn" type="plan" v-model="formInline.cargoSn" @fatherMethods="getCargoInfo"></selectWarehousingCargo>
      </el-form-item>
      <el-form-item label="货物名称">
        <el-input v-model="cargoInfo.cargoName" placeholder="请选择货物名称" disabled />
      </el-form-item>
      <el-form-item label="计划出库数量" prop="plannedNum">
        <el-input-number v-model="formInline.plannedNum" :min="1" :max="cargoInfo.num" />
        <el-tag type="danger" style="margin-left:15px" v-if="cargoInfo.num">{{cargoInfo.cargoName}}库存量{{cargoInfo.num}}</el-tag>
      </el-form-item>
      <el-form-item label="单位">
        <selectUnit v-model="cargoInfo.cargoUnit" disabled></selectUnit>
      </el-form-item>
      <el-form-item label="重量(KG)">
        <el-input v-model="cargoInfo.cargoWeight" placeholder="请输入重量" disabled />
      </el-form-item>
      <el-form-item label="体积(m³)">
        <el-input v-model="cargoInfo.cargoVolume" placeholder="请输入体积" disabled />
      </el-form-item>
      <el-form-item label="规格">
        <el-input v-model="cargoInfo.cargoSpec" placeholder="请输入规格" disabled />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierSn">
        <selectSupplier v-model="formInline.supplierSn" val="supplierSn" :supplier="cargoInfo.supplier"></selectSupplier>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="formInline.remark" :autosize="{ minRows: 4 }" type="textarea" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="loading">
          确 认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import selectWarehousingCargo from "@/components/SystemMain/selectWarehousingCargo.vue";
const { proxy } = getCurrentInstance();
const emit = defineEmits(["fatherMethods"]);
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
let loading = ref(false);
let formInline = ref({
  deliveryNo: "",
  lotNo: "",
  cargoSn: null,
  plannedNum: 1,
  supplierSn: null,
  remark: "",
});
let cargoInfo = ref({});
let dialogVisible = ref(false);
let rules = ref({
  lotNo: [{ required: true, message: "请输入批次号", trigger: "blur" }],
  supplierSn: [{ required: true, message: "请选择供应商", trigger: "blur" }],
  cargoSn: [{ required: true, message: "请选择货物编码", trigger: "blur" }],
  plannedNum: [
    { required: true, message: "请输入计划出库数", trigger: "blur" },
  ],
});
let open = async (e) => {
  if (proxy.$utils.isJSON(e)) {
    formInline.value = proxy.$utils.formatFunction(e, { ...formInline.value });
    formInline.value.id = e.id;
  } else {
    //新增
    formInline.value.deliveryNo = e;
    formInline.value.lotNo = `CK-${Date.now()}`;
  }
  dialogVisible.value = true;
};
let handleClose = () => {
  formInline.value = {
    deliveryNo: "",
    lotNo: "",
    cargoSn: null,
    plannedNum: 1,
    supplierSn: null,
    remark: "",
  };
  proxy.$refs.formEl.resetFields();
  emit("fatherMethods");
  dialogVisible.value = false;
};
let getCargoInfo = (e) => {
  if (e) {
    cargoInfo.value = e;
    //console.log("货物信息", e);
    formInline.value.lotNo = e.batchNumber;
  } else {
    cargoInfo.value = {};
  }
};
let submitForm = async () => {
  await proxy.$refs.formEl.validate((valid, fields) => {
    if (valid) {
      save();
    } else {
      for (let key in fields) {
        return proxy.$utils.showToast(fields[key][0].message, 0);
      }
    }
  });
};
let save = async () => {
  try {
    loading.value = true;
    if (formInline.value.id) {
      await proxy.$apis.deliveryPlanDetailPut(formInline.value);
    } else {
      await proxy.$apis.deliveryPlanDetailAdd(formInline.value);
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
};

defineExpose({ open });
</script>

<style lang="scss" scoped>
</style>
